package cc.woori.mobile;

import java.io.IOException;
import java.text.SimpleDateFormat;

import javax.jdo.PersistenceManager;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cc.woori.mobile.bean.ChurchServiceEntry2;

import com.google.appengine.api.datastore.Text;

@SuppressWarnings("serial")
public class RegisterServiceServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	    throws ServletException, IOException {

	String title = req.getParameter("title");
	String description = req.getParameter("description");
	String pastorName = req.getParameter("pastorName");
	String date = req.getParameter("date");
	String verse = req.getParameter("verse");
	String svcVideoUrl = req.getParameter("videoUrl");
	String svcAudioUrl = req.getParameter("audioUrl");
	String length = req.getParameter("length");

	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

	ChurchServiceEntry2 entry;
	PersistenceManager pm = PMF.get().getPersistenceManager();

	try {
	    entry = new ChurchServiceEntry2(title, pastorName, new Text(
		    description), verse, sdf.parse(date), svcVideoUrl,
		    svcAudioUrl, length);

	    pm.makePersistent(entry);
	} catch (Exception e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	} finally {
	    if (pm != null) {
		pm.close();
	    }

	    RequestDispatcher view = req
		    .getRequestDispatcher("/admin/adminService.jsp");
	    view.forward(req, resp);
	}

    }

}
